package com.example.provider.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.example.api.bean.Book;
import com.example.api.service.BookService;
import com.example.provider.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.smilebookshop.server.service
 * @author: 红雪（hongxue@maihaoche.com）
 * @date: 2019/1/21 5:26 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 * 注意：本内容仅限于卖好车内部传阅，禁止外泄以及用于其他的商业项目
 */
@Service(interfaceClass = BookService.class,version = "1.0.0")
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Override
    @Cacheable(value = "bookdata",key = "'booklist'")
    public List<Book> listAllBooks() {
        return bookMapper.listAllBooks();
    }

    @Override
    @Cacheable(value = "bookdata",key = "'book_id'+#p0")
    public Book getById(int id) {
        System.out.println("调用成功");
        return bookMapper.getById(id).get(0);
    }

    @Override
    @CachePut(value = "bookdata",key = "'book_id'+#book.book_id")
    public Book updateBook(Book book) {
        if (bookMapper.updateBook(book)==1){
            return  book;
        }
        return null;
    }

    @Override
    @CacheEvict(value = "bookdata",key = "'book_id'+#p0",beforeInvocation = true)
    public int deleteBook(int i) {
        return  bookMapper.deleteBook(i);
    }

    @Override
    public Book insertBook(Book book) {
        bookMapper.insertBook(book);
        return book;
    }

}
